using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Blople.GameComponents;


namespace Blople.GameScenes
{
    public class EndScene : GameScene
    {
        SpriteBatch sBatch;
        protected Texture2D backgroundTexture;
        protected InputField inputField;

        protected SpriteFont font;

        int score;
        String message;
        int level;

        public EndScene(Game game, Texture2D backgroundTexture, Texture2D inputTextTexture, Texture2D cursorTexture, SpriteFont font) : base(game)
        {
            this.backgroundTexture = backgroundTexture;
            this.font = font;
            inputField = new InputField(game, inputTextTexture, cursorTexture, font);
            components.Add(inputField);
            message = "";
            score = 0;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            sBatch.Draw(backgroundTexture, new Vector2(0, 0), Color.White);
            sBatch.DrawString(font, "Your score is " + score, new Vector2(125, 100), new Color(118, 58, 0));
            sBatch.DrawString(font, "Enter your name:", new Vector2(110, 350), new Color(118, 58, 0));
            sBatch.DrawString(font, message, new Vector2(55, 500), Color.Red);
            base.Draw(gameTime);
        }

        public int Level
        {
            get { return level; }
            set { level = value; }
        }

        public int Score
        {
            get { return score; }
            set { score = value; }
        }

        public Boolean SaveScoreAndUser() 
        {
            if (!inputField.Validate())
            {
                message = "Minimum of 3 characters";
                return false;
            }
            else
            {
                Save();
                inputField.InputText = "";
                return true;
            }
        }

        private void Save()
        {
            HighScoreData highscores = HighScoreData.LoadHighScores();

            int scoreIndex = -1;
            for (int i = highscores.Count - 1; i > -1 ; i--)
            {
                if (highscores.Level[i] == level)
                {
                    if (score > highscores.Score[i])
                    {
                        scoreIndex = i;
                    }
                }
            }

            if (scoreIndex > -1)
            {
                for (int i = 1; i < 4; i++)
                {
                    if (highscores.Level[scoreIndex] == i)
                    {
                        for (int j = (i * 3) - 1; j > scoreIndex; j--)
                        {
                            highscores.Name[j] = highscores.Name[j - 1];
                            highscores.Score[j] = highscores.Score[j - 1];
                            highscores.Level[j] = highscores.Level[j - 1];
                        }
                    }
                }

                highscores.Name[scoreIndex] = inputField.InputText;
                highscores.Score[scoreIndex] = score;
                highscores.Level[scoreIndex] = level;

                HighScoreData.SaveHighScores(highscores);
            }
        }
        
    }
}
